/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.kn.keywordmatch.controller;

import com.kn.keywordmatch.dto.req.MatchDistanceReq;
import com.kn.keywordmatch.dto.req.VerifyKeywordExpressionReq;
import com.kn.keywordmatch.dto.resp.BaseResp;
import com.kn.keywordmatch.dto.resp.MatchDistanceResp;
import com.kn.keywordmatch.dto.resp.ParseKeywordExpressionResp;
import com.kn.keywordmatch.service.MatchService;
import com.kn.keywordmatch.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@RestController
@Validated
public class BasicController {


    @Autowired
    private PlanService planService;

    @Autowired
    private MatchService matchService;
    /**
     *  邯郸 匹配位置
     * @param reqList
     * @return
     */
    @PostMapping("/matchDistance")
    @ResponseBody
    public List<MatchDistanceResp> matchDistance(@RequestBody @Validated @NotEmpty List<@Valid MatchDistanceReq> reqList) {
        return planService.matchDistance(reqList);
    }

    /**
     *  小极 匹配位置
     * @param reqList
     * @return
     */
    @PostMapping("/matchPoleDistance")
    @ResponseBody
    public List<MatchDistanceResp> matchPoleDistance(@RequestBody @Validated @NotEmpty(message = "入参不能为空") List<@Valid MatchDistanceReq> reqList) {
        return planService.matchPoleDistance(reqList);
    }

    /**
     *  定向匹配
     * @param reqList
     * @return
     */
    @PostMapping("/matchByDirection")
    @ResponseBody
    public List<MatchDistanceResp> matchByDirection(@RequestBody @Validated @NotEmpty List<@Valid MatchDistanceReq> reqList) {
        return planService.matchByDirection(reqList);
    }

    /**
     *  关键字匹配
     * @param reqList
     * @return
     */
    @PostMapping("/matchByKeyword")
    @ResponseBody
    public List<MatchDistanceResp> matchByKeyword(@RequestBody @Validated @NotEmpty List<@Valid MatchDistanceReq> reqList) {
        return planService.matchByKeyword(reqList);
    }

    @PostMapping("/sendMockData/{routeKey}")
    @ResponseBody
    public BaseResp sendMockData(@PathVariable("routeKey") String routeKey,
                                @RequestBody @Validated @NotEmpty List<@Valid MatchDistanceReq> reqList) {
        return planService.sendMockData(routeKey,reqList);
    }

    // 校验表达式
    @PostMapping("/verifyKeywordExpression")
    @ResponseBody
    public BaseResp<String> verifyKeywordExpression(@RequestBody @Validated VerifyKeywordExpressionReq req) {
        return matchService.verifyKeywordExpression(req.getExpression());
    }

    // 解析表达式
    @PostMapping("/analysisKeywordExpression")
    @ResponseBody
    public BaseResp<ParseKeywordExpressionResp> analysisKeywordExpression(@RequestBody @Validated VerifyKeywordExpressionReq req) {
        return matchService.analysisKeywordExpression(req.getExpression(),null);
    }

    @PostMapping("/matchKeywordByExpression")
    @ResponseBody
    public List<MatchDistanceResp> matchKeywordByExpression(@RequestBody @Validated @NotEmpty List<@Valid MatchDistanceReq> reqList) {
        return matchService.matchKeywordByExpression(reqList);
    }



}

